package com.b1502.store2.fragment;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.b1502.store2.R;
import com.b1502.store2.activity.NewsDetailActivity;
import com.b1502.store2.adapter.NewsAdapter;
import com.b1502.store2.model.bean.NewsBean;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private View view;
    private RecyclerView rv_message_news;
    private LinearLayoutManager linearLayoutManager;
    private NewsAdapter newsAdapter;

    public NewsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NewsFragment newInstance(String param1, String param2) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_news, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        int spacingInPixels=8;

        rv_message_news = (RecyclerView) view.findViewById(R.id.rv_message_news);

        rv_message_news.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

        linearLayoutManager = new LinearLayoutManager(getActivity());

        rv_message_news.setLayoutManager(linearLayoutManager);

        newsAdapter = new NewsAdapter(getActivity());

        getNews("newsId","648cf203-16f0-4603-b291-40864c472356");

//        newsAdapter.setOnItemClickLitener(new NewsAdapter.OnItemClickLitener()
//        {
//
//            @Override
//            public void onItemClick(View view, int position)
//            {
//
//                Intent intent=new Intent(getActivity(), NewsDetailActivity.class);
//
//                intent.putExtra("userid",list.get(position).getId());
//
//                getActivity().startActivity(intent);
//
//            }
//
//            @Override
//            public void onItemLongClick(View view, int position)
//            {
//                Toast.makeText(getActivity(), position + " long click",
//                        Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    class SpacesItemDecoration extends RecyclerView.ItemDecoration{

        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;

            if (parent.getChildPosition(view)==0){

                outRect.top=space;

            }

        }
    }

    private void getNews(String param, String newsId) {

        StoreParams params = new StoreParams(UrlUtil.GetNewsList,param,newsId);

        Log.d("pppp",params+"");

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //Log.d(TAG, result);

                Gson gson=new Gson();

                NewsBean[] newsBean = gson.fromJson(result, NewsBean[].class);

                final List<NewsBean> list=new ArrayList<>();

                for (NewsBean nb : newsBean) {

                    NewsBean newsbean=new NewsBean();

                    newsbean.setTitle(nb.getTitle());

                    newsbean.setId(nb.getId());

                    Log.d("userid",nb.getId()+"");

                    newsbean.setImgUrl(nb.getImgUrl());

                    list.add(newsbean);

                }

                rv_message_news.setAdapter(newsAdapter);

                newsAdapter.addRest(list);

                newsAdapter.setOnItemClickLitener(new NewsAdapter.OnItemClickLitener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        Intent intent=new Intent(getActivity(), NewsDetailActivity.class);

                        intent.putExtra("userid",list.get(position).getId());

                        getActivity().startActivity(intent);

                    }

                    @Override
                    public void onItemLongClick(View view, int position) {

                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
